package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    /**
     * 根据条件分页查询护理项目
     *
     * @param name     护理项目的名称
     * @param pageNum  当前页
     * @param pageSize 每页显示记录数
     * @param status   状态
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> list(String name, Integer pageNum, Integer pageSize, Byte status) {
        //1. 设置分页条件
        PageHelper.startPage(pageNum, pageSize);

        //2. 调用mapper的动态sql查询数据
        Page<NursingProjectVo> page = nursingProjectMapper.list(name, status);

        //3. 封装数据
        PageResponse response = new PageResponse();
        response.setPage(pageNum);
        response.setPageSize(pageSize);
        response.setTotal(page.getTotal());
        response.setRecords(page.getResult());

        return response;
    }

    /**
     * 新增护理项目
     *
     * @param dto
     */
    @Override
    public void save(NursingProjectDto dto) {
        NursingProject  nursingProject = new NursingProject();
        BeanUtils.copyProperties(dto, nursingProject);
        nursingProjectMapper.save(nursingProject);
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo queryById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.queryById(id);
        NursingProjectVo vo = new NursingProjectVo();
        BeanUtils.copyProperties(nursingProject, vo);
        return vo;
    }

    /**
     * 根据id更新
     *
     * @param dto
     */
    @Override
    public void update(NursingProjectDto dto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(dto, nursingProject);
        nursingProjectMapper.update(nursingProject);
    }
}
